﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Dynamics.AX.Framework.Tools.ModelManagement;

namespace AxUtilUI
{
    public class AxUtilCalls
    {
        string sqlServerName;
        string sqlDbName;

        public string SqlServerName
        {
            get
            {
                if (!string.IsNullOrEmpty(sqlServerName))
                {
                    return sqlServerName;
                }
                else
                {
                    throw new Exception("Sql server name is not provided.");
                }
            }
        }

        public string SqlDbName
        {
            get
            {
                if (!string.IsNullOrEmpty(sqlDbName))
                {
                    return sqlDbName;
                }
                else
                {
                    throw new Exception("Sql server database name is not provided.");
                }
            }
        }

        public AxUtilCalls(string sqlServerName, string sqlDbName)
        {
            this.sqlServerName = sqlServerName;
            this.sqlDbName = sqlDbName;
        }

        public IList<ModelManifest> ListModels(out AxUtilContext context)
        {
            context = new AxUtilContext();
            AxUtilConfiguration config = new AxUtilConfiguration();
            config.Server = SqlServerName;
            config.Database = SqlDbName;
            AxUtil axUtil = new AxUtil(context, config);
            return axUtil.List(context, config);
        }

        public ModelContents ViewFile(string modelFilename, bool verbose, out AxUtilContext context)
        {
            context = new AxUtilContext();
            AxUtilConfiguration config = new AxUtilConfiguration();
            config.Server = SqlServerName;
            config.Database = SqlDbName;
            AxUtil axUtil = new AxUtil(context, config);
            return axUtil.View(modelFilename, verbose);
        }

        public ModelContents ViewModel(int modelId, bool verbose, out AxUtilContext context)
        {
            context = new AxUtilContext();
            AxUtilConfiguration config = new AxUtilConfiguration();
            config.Server = SqlServerName;
            config.Database = SqlDbName;
            AxUtil axUtil = new AxUtil(context, config);
            ModelArgument modelArg = new ModelArgument(modelId);
            return axUtil.View(modelArg, verbose);
        }

        public void ExportModel(int modelId, string exportFile, out AxUtilContext context)
        {
            context = new AxUtilContext();
            ModelArgument modelArg = new ModelArgument(modelId);
            AxUtilConfiguration config = new AxUtilConfiguration();
            config.ExportFile = exportFile;
            config.Server = SqlServerName;
            config.Database = SqlDbName;
            config.ModelArgument = modelArg;
            AxUtil axUtil = new AxUtil(context, config);
            axUtil.Export(context, config);
        }

        public void ExportModelStore(string exportFile, out AxUtilContext context)
        {
            context = new AxUtilContext();
            AxUtilConfiguration config = new AxUtilConfiguration();
            config.ExportFile = exportFile;
            config.Server = SqlServerName;
            config.Database = SqlDbName;
            AxUtil axUtil = new AxUtil(context, config);
            axUtil.ExportStore(context, config, false);
        }

        public IList<ModelContents> ImportModels(IList<string> importFiles, out AxUtilContext context)
        {
            context = new AxUtilContext();
            AxUtilConfiguration config = new AxUtilConfiguration();
            foreach (string importFile in importFiles)
            {
                config.ImportFiles.Add(importFile);
            }
            config.Server = SqlServerName;
            config.Database = SqlDbName;
            AxUtil axUtil = new AxUtil(context, config);
            return axUtil.Import(context, config);
        }

        public ModelContents ImportModel(string importFile, bool createparents, ConflictModelResolverType conflictResolverType, out AxUtilContext context)
        {
            context = new AxUtilContext();
            AxUtilConfiguration config = new AxUtilConfiguration();
            config.ImportFiles.Add(importFile);
            config.Server = SqlServerName;
            config.Database = SqlDbName;
            config.CreateParents = createparents;
            config.ConflictResolver = conflictResolverType;
            AxUtil axUtil = new AxUtil(context, config);
            IList<ModelContents> contents = axUtil.Import(context, config);
            return contents == null || contents.Count == 0 ? null : contents[0];
        }

        public void DeleteModel(int modelId, out AxUtilContext context)
        {
            context = new AxUtilContext();
            ModelArgument modelArg = new ModelArgument(modelId);
            AxUtilConfiguration config = new AxUtilConfiguration();
            config.Server = SqlServerName;
            config.Database = SqlDbName;
            config.ModelArgument = modelArg;
            AxUtil axUtil = new AxUtil(context, config);
            axUtil.Delete(context, config);
        }
    }
}
